﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Resources;
using System.ComponentModel;
using VIBlend.WPF.Controls;
using System.Reflection;
using System.Data;
using AnonsaiWPF.NwindDataSetTableAdapters;

namespace AnonsaiWPF.CSharp.DataGridViewDemos
{
    [Category("VIBlend.WPF.Examples")]
    public partial class VirtualModeDemo : Window, IWindow
    {
        #region IWindow Members
        HierarchyItem oldItem;

        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

        public Grid Root
        {
            get
            {
                return this.LayoutRoot;
            }
        }
        #endregion

        public VirtualModeDemo()
        {
            InitializeComponent();
      
            HierarchyItem currentColumn = dataGrid1.ColumnsHierarchy.Items.Add("Id");
            HierarchyItem nameColumn = dataGrid1.ColumnsHierarchy.Items.Add("Name");
            HierarchyItem dateColumn = dataGrid1.ColumnsHierarchy.Items.Add("Date");
            HierarchyItem productColumn = dataGrid1.ColumnsHierarchy.Items.Add("Product");
            HierarchyItem quantityColumn = dataGrid1.ColumnsHierarchy.Items.Add("Quantity");
            HierarchyItem unitPriceColumn = dataGrid1.ColumnsHierarchy.Items.Add("UnitPrice");
            HierarchyItem salesAmountColumn = dataGrid1.ColumnsHierarchy.Items.Add("Sales Amount");
       
            foreach (HierarchyItem item in this.dataGrid1.ColumnsHierarchy.Items)
            {
                item.Width = 100;
                item.CellStyle = Application.Current.Resources["TextCenteredCellStyle"] as Style;
            }

            dateColumn.Width = 150;
            productColumn.Width = 150;

            for (int i = 0; i < 50000; i++)
            {
                HierarchyItem row = this.dataGrid1.RowsHierarchy.Items.Add("");
                row.CellDataSource = GridCellDataSource.Virtual;
            }

            dataGrid1.SelectionMode = VIBlend.WPF.Controls.DataGrid.SELECTION_MODE.FULL_ROW_SELECT;
            dataGrid1.ColumnsHierarchy.AllowResize = false;
            dataGrid1.RowsHierarchy.SetColumnWidth(0, 25);
            
            this.dataGrid1.Refresh();
            this.dataGrid1.DeferredScrollingEnabled = true;
            this.dataGrid1.CellValueNeeded += new VIBlend.WPF.Controls.DataGrid.CellValueNeededEventHandler(pivotGrid1_CellValueNeeded);
            PrepareGridData();
            this.dataGrid1.SelectionMode = VIBlend.WPF.Controls.DataGrid.SELECTION_MODE.FULL_ROW_SELECT;
            this.dataGrid1.MultipleSelectionEnabled = false;
            this.dataGrid1.RowsHierarchy.SetColumnWidth(0, 25);
            this.dataGrid1.RowsHierarchy.AllowResize = false;
        }

        void pivotGrid1_CellValueNeeded(object sender, CellValueNeededEventArgs args)
        {
            switch (args.ColumnItem.Text)
            {
                case "Id":
                    args.CellValue = args.RowItem.ItemIndex;
                    break;
                case "Name":
                    args.CellValue = this.listSaleRecord[args.RowItem.ItemIndex].Name;
                    break;
                case "Date":
                    args.CellValue = this.listSaleRecord[args.RowItem.ItemIndex].Date;
                    break;
                case "Product":
                    args.CellValue = this.listSaleRecord[args.RowItem.ItemIndex].Product;
                    break;
                case "Quantity":
                    args.CellValue = this.listSaleRecord[args.RowItem.ItemIndex].Quantity;
                    break;
                case "UnitPrice":
                    args.CellValue = this.listSaleRecord[args.RowItem.ItemIndex].UnitPrice;
                    break;
                case "Sales Amount":
                    args.CellValue = this.listSaleRecord[args.RowItem.ItemIndex].SalesAmount;
                break;
            }

        }

        #region Sample Data

        #region Data
        string[] firstNames = new string[] 
        {
            "Andrew",
            "Nancy",
            "Shelley",
            "Regina",
            "Yoshi",
            "Antoni",
            "Mayumi",
            "Ian",
            "Peter",
            "Lars",
            "Petra",
            "Martin",
            "Sven",
            "Elio",
            "Beate",
            "Cheryl",
            "Michael",
            "Guylène"
        };

        string[] lastNames = new string[] 
        {
            "Fuller",
            "Davolio",
            "Burke",
            "Murphy",
            "Nagase",
            "Saavedra",
            "Ohno",
            "Devling",
            "Wilson",
            "Peterson",
            "Winkler",
            "Bein",
            "Petersen",
            "Rossi",
            "Vileid",
            "Saylor",
            "Björn",
            "Nodier"
        };

        static string[,] productNames = new string[,] 
        {
            {"Black Tea", "1.95"},
            {"Green Tea", "1.95"},
            {"Caffè Espresso", "1.45"},
            {"Doubleshot Espresso", "1.75"},
            {"Caffè Latte", "2.25"},
            {"White Chocolate Mocha", "2.35"},
            {"Caramel Latte", "2.35"},
            {"Caffè Americano", "1.65"},
            {"Cappuccino", "2.10"},
            {"Espresso Truffle", "2.45"},
            {"Espresso con Panna", "1.81"},
            {"Peppermint Mocha Twist", "1.99"}
        };

        public class EmployeeSaleRecord
        {
            public EmployeeSaleRecord(string Name, DateTime Date, int ProductId, int Quantity)
            {
                this._name = Name;
                this._date = Date;
                this._productid = ProductId;
                this._quantity = Quantity;
            }

            #region Private Members
            private string _name;
            private DateTime _date;
            private int _productid;
            private int _quantity;
            #endregion

            #region Properties

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            public DateTime Date
            {
                get { return _date; }
                set { _date = value; }
            }

            public string Product
            {
                get { return productNames[_productid, 0]; }
            }

            public int Quantity
            {
                get { return _quantity; }
                set { _quantity = value; }
            }

            public double UnitPrice
            {
                get
                {
                    double unitPrice = double.Parse(productNames[_productid, 1]);
                    return unitPrice;
                }
            }

            public double SalesAmount
            {
                get { return (double)_quantity * UnitPrice; }
            }


            #endregion
        }


        #endregion

        public List<EmployeeSaleRecord> listSaleRecord = new List<EmployeeSaleRecord>();

        private void PrepareGridData()
        {
            const int rowsToLoad = 50000;
            listSaleRecord.Clear();

            Random rand = new Random();

            for (int i = 0; i < rowsToLoad; i++)
            {
                int productId = rand.Next(0, productNames.Length / 2 - 1);
                int quantity = rand.Next(1, 5);
                listSaleRecord.Add(new EmployeeSaleRecord(
                    string.Format("{0} {1}",
                    firstNames[rand.Next(0, firstNames.Length - 1)],
                    lastNames[rand.Next(0, lastNames.Length - 1)]),
                    DateTime.Now.AddDays(-rand.Next(10, 100)),
                    productId,
                    quantity)
                    );
            }
        }

        #endregion
        
    
        public void SetTheme()
        {
            dataGrid1.FontFamily = new FontFamily("Tahoma");
        }
    } 


}
